package com.forlove.app.common.widget.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.forlove.app.R;
import com.forlove.app.common.utils.LogUtil;


/**
 * 可弹出物基类. 封装基本的对话框能力，及切入切出动画。由子类根据需求填充contentView，同时也对外暴露setContentView的能力，
 * 当现有dialog组件不能满足需求时，不推荐由manager层直接setContentView，推荐新建子类，通过子类setContentView。
 *
 * @author liuweining
 */
public class QyqBaseDialog {

    private static final String TAG = "YimeiBaseDialog";

    private static final int DISMISS_DURATION = 1500;

    protected Activity mActivity; // context
    protected AlertDialog.Builder mBuilder; // builder
    protected Dialog mDialog; // dialog
    protected Window mWindow; // window

    protected View mContainerView; // RootView
    protected ViewGroup mContentView; // contentView, maybe equal mContaierView
    protected TextView mPositiveBtn; // OKBtn, maybe null
    protected TextView mNegativeBtn; // CancelBtn, maybe null

    /**
     * 进入动画是windowAnimation windowAnimation定义在style中.
     */
    protected int[] mAnimations = {R.style.QYQ_Dialog_Animation_Fly,
            R.style.QYQ_Dialog_Animation_Fade,
            R.style.QYQ_Toast_Animation_Fly,
            R.style.QYQ_Toast_Animation_Fade,
            R.style.QYQ_Dialog_Animation_FlyInFadeOut,
            R.style.QYQ_Dialog_Animation_FlyInFlyOut,
            R.style.QYQ_Toast_Animation_FlyInFlyOut,
            R.style.QYQ_Toast_Animation_FlyTopInFlyTopOut};

    protected static Handler sHandler = new Handler(Looper.getMainLooper());


    private BaseDialogStatusChangeListener stateListener;

    /**
     * 构造函数.
     *
     * @param activity context
     */
    public QyqBaseDialog(Activity activity) {
        if (null != activity && !activity.isFinishing()) {
            try {
                mActivity = activity;

                // 构建dialog
                mBuilder = new AlertDialog.Builder(mActivity, R.style.FullScreenDialog);

                mDialog = mBuilder.create();
                mDialog.setOwnerActivity(mActivity);
                // mDialog.setCancelable(false);

                mWindow = mDialog.getWindow();

                // 设置切入切出动画
//                mWindow.setWindowAnimations(mAnimations[AnimationType.DIALOG_FLY_IN_FLY_OUT
//                        .getValue()]);
//                mWindow.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
            } catch (Exception e) {
                LogUtil.e(TAG, e.getMessage(), e);
            }
        }
    }

    public void addDissmissEventListener(BaseDialogStatusChangeListener stateListener) {
        this.stateListener = stateListener;
    }

    /**
     * 填充内容.
     *
     * @param view 填充的view
     */
    public void setContentView(View view) {
        if (null != mContentView) {
            // 删除所有子view
            mContentView.removeAllViews();
            // 添加新的view
            mContentView.addView(view);
        }
    }

    /**
     * 显示弹窗.
     *
     * @param isAutoDismiss 是否自动消失
     */
    public void show(boolean isAutoDismiss) {
        if ((null != mActivity
                && !mActivity.isFinishing())
                && null != mContainerView
                && mDialog != null) {
            try {
                mDialog.show();
                mDialog.setContentView(mContainerView);
//                mWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
                // 自动隐藏dialog
                if (isAutoDismiss) {
                    sHandler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            dismiss();
                        }
                    }, DISMISS_DURATION);
                }
            } catch (Exception e) {
                LogUtil.e(TAG, e.getMessage(), e);
            }
        }

    }

    /**
     * 显示弹窗.
     *
     * @param isAutoDismiss 是否自动消失
     * @param layoutParams  样式
     */
    public void show(boolean isAutoDismiss, ViewGroup.LayoutParams layoutParams) {
        if (null != mContainerView) {
            try {
                mDialog.show();
                if (layoutParams != null) {
                    mDialog.setContentView(mContainerView, layoutParams);
                } else {
                    mDialog.setContentView(mContainerView);
                }
                // 自动隐藏dialog
                if (isAutoDismiss) {
                    sHandler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            dismiss();
                        }
                    }, DISMISS_DURATION);

                }
            } catch (Exception e) {
                LogUtil.e(TAG, e.getMessage(), e);
            }
        }

    }

    /**
     * 设置dialog是否可被取消.
     *
     * @param isCancelable 是否能被取消
     */
    public void setDialogCancelable(boolean isCancelable) {
        if (null != mDialog) {
            mDialog.setCancelable(isCancelable);
        }
    }

    /**
     * dismiss窗口.
     */
    public void dismiss() {
        dismiss(AnimationType.DIALOG_FADE);
    }

    /**
     * dismiss窗口.
     *
     * @param animationType 推出动画类型
     */
    public void dismiss(AnimationType animationType) {
        try {
            if (null == mDialog || null == mActivity) {
                return;
            }

            if (mDialog.isShowing() && !mActivity.isFinishing()) {
                setOutAnimation(animationType);
                mDialog.dismiss();
                LogUtil.d(TAG, "dismiss dialog");
            }
        } catch (Exception e) {
            LogUtil.e(TAG, e.getMessage(), e);
        } finally {
            if (stateListener != null) {
                stateListener.onDismiss();
            }
        }
    }

    /**
     * 获取inflater.
     *
     * @return layoutInflater
     */
    protected LayoutInflater getLayoutInflater() {
        if (null == mActivity) {
            return null;
        }
        return mActivity.getLayoutInflater();
    }

    /**
     * dialog 是否正在显示中.
     *
     * @return true 显示中
     */
    public boolean isShowing() {
        if (null != mDialog) {
            return mDialog.isShowing();
        }
        return false;
    }

    protected void setInAnimation(AnimationType animationType) {
        if (null != mWindow) {
            mWindow.setWindowAnimations(mAnimations[animationType.getValue()]);
        }
    }

    protected void setOutAnimation(AnimationType animationType) {
        if (null != mWindow) {
            mWindow.setWindowAnimations(mAnimations[animationType.getValue()]);
        }
    }
}
